package xwj.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import xwj.example.demo.domain.Product;
import xwj.example.demo.domain.Supplier;
import xwj.example.demo.dao.SupplierDao;
import xwj.example.demo.exception.ProductAdminException;
import xwj.example.demo.service.ISupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwj
 * @since 2022-10-30
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierDao, Supplier> implements ISupplierService {
    /**
     * 添加商品
     *
     * @param supplier
     * @return
     */
    public Supplier addSupplier(Supplier supplier) throws ProductAdminException {
        if (supplier.getId() != null) {
            if (getById(supplier.getId())!=null) {
                throw new ProductAdminException("添加供应商失败：商品已经存在");
            }
        }
        this.baseMapper.insert(supplier);
        return supplier;
    }

    /**
     * 根据Id查找
     *
     * @param id
     * @return
     */
    public Supplier getSupplier(long id) {
        return getById(id);
    }
    /**
     * 更新商品信息
     *
     * @param id
     * @param supplier
     */
    public void updateSupplier(long id, Supplier supplier) throws ProductAdminException {
        if (getById(id)==null) {
            throw new ProductAdminException("修改商品失败：商品不存在");
        }
        this.baseMapper.updateById(supplier);
    }

    /**
     * 删除商品
     *
     * @param id
     */
    public void deleteSupplier(long id) {
        this.removeById(id);
    }

    public IPage<Supplier> selectSupplierPage(String name,Integer pageNum,Integer pageSize){
        Page<Supplier> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Supplier> qw=new QueryWrapper();
        qw.like("name",name);
        this.baseMapper.selectPage(page,qw);
        return page;
    }
}
